<?php
/*
 * ===============================================================
 * 活动处理
 * ===============================================================
*/
if(!defined('IN_UCHOME')) {
	exit('Access Denied');
}
foreach(array('0~10人','11~20人','21~30人','31~40人','41~50人','不限') as $key=>$val){
	$activityScale .= '<option value="'.$val.'" '.($_POST['scale'] == $val ? 'selected="selected"' : '').'>'.$val.'</option>';
}
foreach (array('抽奖活动','推广活动') as $key=>$val){
	$activityClassis .= '<option value="'.$val.'" '.($_POST['classis'] == $val ? 'selected="selected"' : '').'>'.$val.'</option>';
}

//默认活动城市
$resideprovince = '重庆';
$residecity = '渝北';

$op = $_GET['op'] ? $_GET['op'] : '';

if($op != 'find'){
	if(submitcheck('activitysubmit')){
	$result = checkData();
	if(!is_array($result[0])){
		foreach ($result[1] as $key=>$value){
			$errors[$key] = '<font color="red">'.$value.'</font>';
		}
		$setarr = $result[2];
	}else{
		$setarr = $result[0];
		//保存活动封面的图片
		if($_FILES['pic']['size'] && $filearr = pic_save($_FILES['pic'], -1)) {
			$setarr['a_pic'] = $filearr['filepath'];
		}
		//检查图片的尺寸大小
		$sizes = array('width'=>'588','height'=>'361');
		if($setarr['a_pic']){
			$picSizes = getimagesize($_SC['attachdir'].'./'.$filearr['filepath']);
			($picSizes[0] < $size['height'] || $picSizes[1] < $sizes['width']) && showmessage('pic_size_error', "", 1);
		}
		//保存抽奖信息,如果有抽奖
		if($_POST['lottery']){
			$lottery['l_integral'] = (int)$_POST['integral'];
			$lottery['l_area'] = (int)$_POST['sarea'] . '-' .(int)$_POST['earea'];
			$lottery['l_gifts'] = $lottery['l_surplus'] = (int)$_POST['prizenum'];
			$lastid = inserttable('lottery', $lottery, 1);
		}
		if(($lastid && $_POST['lottery']) || !$_POST['lottery']){
			$setarr['a_lotteryid'] = $lastid;
			$setarr['uid'] = $_SGLOBAL['supe_uid'];
			$setarr['uname'] = $_SGLOBAL['supe_username'];
			$setarr['a_ontime'] = time();
			$lastid = inserttable('activity', $setarr, 1);
			if($lastid){
				showmessage('do_success', "cp.php", 1);
			}else{
				showmessage('activity_error', "", 1);
			}
		}else{
			showmessage('activity_error', "", 1);
		}
	}
	}
}else{
	if(submitcheck('findsubmit')){
		$result = checkDataOfFind();
		if(!is_array($result[0])){
			foreach ($result[1] as $key=>$value){
				$errors[$key] = '<font color="red">'.$value.'</font>';
			}
		}else{
			$setarr = $result[0];
			//保存活动封面的图片
			if($_FILES['pic']['size'] && $filearr = pic_save($_FILES['pic'], -1)) {
				$setarr['o_pic'] = $filearr['filepath'];
			}
			$setarr['uid'] = $_SGLOBAL['supe_uid'];
			$setarr['uname'] = $_SGLOBAL['supe_username'];
			$setarr['o_ontime'] = time();
			$lastid = inserttable('otheractivity', $setarr, 1);
			if($lastid){
				showmessage('do_success', "cp.php", 1);
			}else{
				showmessage('activity_error', "", 1);
			}
		}
	}
}

include_once template("cp_activity");

//验证用户提交的数据
function checkData(){
	//验证标题字数
	(mb_strlen($_POST['title'],'utf-8') < 8 || mb_strlen($_POST['title'],'utf-8') > 80) && $error['title'] = '活动标题长度不得少于8字或大于80字';
	$error['title'] || $setarr['a_topic'] = getstr($_POST['title'], 80, 1, 1, 1);
	//验证活动地址长度
	(mb_strlen($_POST['address'],'utf-8') < 8 || mb_strlen($_POST['address'],'utf-8') > 120) && $error['address'] = '活动地址长度不得少于8字或大于120字';
	$error['address'] || $setarr['a_address'] = getstr($_POST['address'], 120, 1, 1, 1);
	//验证活动城市是否填写
	($_POST['resideprovince'] || $_POST['residecity']) == '' && $error['city'] = '请选填活动所在的城市';
	$error['resideprovince'] || $setarr['a_city'] = getstr($_POST['resideprovince'], 40, 1, 1, 1);
	$error['residecity'] || $setarr['a_city'] .= ' ' . getstr($_POST['residecity'], 40, 1, 1, 1);
	//验证活动结束时间
	$_POST['enddate'] == '' && $error['enddate'] = '请选择活动的结束时间';
	$error['enddate'] || $setarr['a_endtime'] = strtotime($_POST['enddate']);
	//验证活动的规模
	$_POST['scale'] == '' && $error['scale'] = '活动规模必选';
	$error['scale'] || $setarr['a_scale'] = getstr($_POST['scale'], 20, 1, 1, 1);
	//验证活动类别
	!$_POST['classis'] && $error['classis'] = '必须指定活动类别';
	$error['classis'] || $setarr['a_classis'] = (int)$_POST['classis'];
	//检测是否开启轮盘模式
	if($_POST['lottery']){
		//验证轮盘每次的消费积分
		!(int)$_POST['integral'] && $error['integral'] == '必须设定每次参与轮盘抽奖所消耗的积分';
		//验证中间区域是否存在
		(!(int)$_POST['sarea'] || !(int)$_POST['earea'] || (int)$_POST['earea'] < (int)$_POST['sarea']) && $error['area'] = '中奖区域设定错误';
		//验证中奖礼品数是否存在
		!(int)$_POST['prizenum'] && $error['prizenum'] = '中奖礼品数设定错误';
	}
	//验证活动开展方式
	$setarr['a_manner'] = (int)$_POST['manner'];
	//是否显示期望
	$setarr['a_isexpect'] = (int)$_POST['expect'];
	//验证推广入选人数
	!(int)$_POST['peoples'] && $error['peoples'] = '推广入选人数必须是正整数';
	$setarr['a_selecteds'] = (int)$_POST['peoples'];
	//验证联系人
	!$_POST['contactname'] && $error['contactname'] = '联系人必须填写';
	$setarr['a_contact'] = getstr($_POST['contactname'], 20, 1, 1, 1);
	//验证联系电话
	!$_POST['contacttel'] && $error['contacttel'] = '联系人电话必须填写';
	$setarr['a_contacttel'] = getstr($_POST['contacttel'], 20, 1, 1, 1);
	//验证联系QQ
	!preg_match('/^\d{5,12}$/', $_POST['contactqq']) && $error['contactqq'] = 'QQ号码格式错误';
	$setarr['a_contactqq'] = getstr($_POST['contactqq'], 20, 1, 1, 1);
	//验证活动说明
	mb_strlen($_POST['content']) < 50 && $error['content'] = '活动说明至少50个字';
	include_once(S_ROOT.'./source/function_blog.php');
	$desc = $_POST['content'];
	$desc = checkhtml($desc);
	$desc = getstr($desc, 0, 1, 0, 1, 0, 1);
	$desc = preg_replace(array(
			"/\<div\>\<\/div\>/i",
			"/\<a\s+href\=\"([^\>]+?)\"\>/i"
	), array(
			'',
			'<a href="\\1" target="_blank">'
	), $desc);
	$setarr['a_directions'] = $_POST['content'];
	$setarr['a_ontime'] = time();
	$setarr['a_status'] = $setarr['a_views'] = $setarr['a_shares'] = $setarr['a_participates'] = 0;
	if($error){
		return array('error',$error,$setarr);
	}else{
		return array($setarr);
	}
}
//验证发现活动提交的数据
function checkDataOfFind(){
	//验证标题字数
	(mb_strlen($_POST['title'],'utf-8') < 8 || mb_strlen($_POST['title'],'utf-8') > 80) && $error['title'] = '活动标题长度不得少于8字或大于80字';
	$error['title'] || $setarr['o_title'] = getstr($_POST['title'], 80, 1, 1, 1);
	//验证活动地址长度
	(!preg_match('/^https?:\/\/([A-z0-9]+[_\-]?[A-z0-9]+\.)*[A-z0-9]+\-?[A-z0-9]+\.[A-z]{2,}(\/.*)*\/?$/', $_POST['address'])) && $error['address'] = '活动链接地址格式错误';
	$error['address'] || $setarr['o_url'] = getstr($_POST['address'], 120, 1, 1, 1);
	//验证活动城市是否填写
	($_POST['resideprovince'] || $_POST['residecity']) == '' && $error['city'] = '请选填活动所在的城市';
	$error['resideprovince'] || $setarr['o_city'] = getstr($_POST['resideprovince'], 40, 1, 1, 1);
	$error['residecity'] || $setarr['o_city'] .= ' ' . getstr($_POST['residecity'], 40, 1, 1, 1);
	//验证活动结束时间
	$_POST['enddate'] == '' && $error['enddate'] = '结束时间格式错误';
	$error['enddate'] || $setarr['o_endtime'] = strtotime($_POST['enddate']);
	//验证活动说明
	mb_strlen($_POST['content']) < 20 && $error['content'] = '活动说明至少20个字';
	$setarr['o_content'] = getstr($_POST['content'], 0, 1, 1, 1);
	if($error){
		return array('error',$error,$setarr);
	}else{
		return array($setarr);
	}
}